package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import gs.*;
	/**
	 * класс заднего фона
	 * @author 
	 */
	public class Background extends Sprite 
	{
		static private const GROUND_SPEED:Number = 15;
		static private const SKY_SPEED:Number = 5;
		
		public var ground:Groundsymb = new Groundsymb(); //земля 1 слой, который движется быстро
		public var sky:Skysymb = new Skysymb(); //небо с облаками, двигаются медленно
		public var friendcastle:FriendCastle = new FriendCastle();
		public var enemycastle:EnemyCastle = new EnemyCastle();
		public var actionTimer:Timer = new Timer(25);
		
		public function Background() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init)
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			actionTimer.addEventListener(TimerEvent.TIMER, on_timer);
			actionTimer.start();
			addChild(sky);
			addChild(ground);
			ground.addChild(friendcastle);
			ground.addChild(enemycastle);
			enemycastle.x = ground.width - enemycastle.height;
			ground.y = sky.height;
		}
		
		private function on_timer(e:TimerEvent):void 
		{
			if (Main.a_pressed && (ground.x < 0) && (sky.x < 0))
			{
				TweenLite.to(ground,  actionTimer.delay / 1000  /*миллисекунды*/, { x: ground.x + GROUND_SPEED } );
				TweenLite.to(sky,  actionTimer.delay / 1000 , { x: sky.x + SKY_SPEED } );
			}
			if (Main.d_pressed && (ground.x > Main.GAME_WIDTH - ground.width) && (sky.x > Main.GAME_WIDTH - sky.width))
			{
				TweenLite.to(ground,  actionTimer.delay / 1000 , { x: ground.x - GROUND_SPEED } );
				TweenLite.to(sky,  actionTimer.delay / 1000 , { x: sky.x - SKY_SPEED } );
			}
		}
		
		
		
	}

}